﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_Admin_Admin_QuanLyThongTinSieuThi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            btnXoaLogo.Attributes.Add("onclick", " return confirm('Chắc chắn xóa?');");
            DataDSTableAdapters.THIETLAP_WEBSITETableAdapter tlAdapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
            DataList1.DataSource = tlAdapter.THIETLAP_WEBSITE_LayThongTinTheoTenNhom("ThongTinSieuThi");
            DataList1.DataBind();
        }
    }
    protected void btnUploadLogo_Click(object sender, EventArgs e)
    {
        string filename = FileUploadLogo.FileName;
        if (filename == string.Empty)
        {
            //Response.Write("<script>alert('Invalid input path');</script>");
            return;
        }
        if (!FileUploadLogo.HasFile)
        {
            //Response.Write("<script>alert('Invalid file name');</script>");
            return;
        }
        int index = filename.LastIndexOf(".");
        string name = filename.Substring(0, index);
        string extension = filename.Substring(index + 1, filename.Length - index - 1);
        //Response.Write("<script>alert('" + extension + "');</script>");
        if (extension != "gif" && extension != "png" && extension != "bmp" && extension != "jpg" && extension != "jpeg")
        {

            Response.Write("<script>alert('Chỉ cho phép upload file hình ảnh dạng png, bmp, jpg, jpeg');</script>");
            return;
        }
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER2"].ToString();

        DataDSTableAdapters.THIETLAP_WEBSITETableAdapter adapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
        DataDS.THIETLAP_WEBSITEDataTable table = adapter.THIETLAP_WEBSITE_LayTheoNhomVaGiaTri("Logo", imageFolder+filename);
        if (table.Count != 0)
        {
            Response.Write("<script>alert('Tên tập tin đã tồn tại trên server!');</script>");
            return;
        }
        string path = Server.MapPath(ConfigurationSettings.AppSettings["IMAGE_FOLDER"].ToString());
        path += filename;
        FileUploadLogo.SaveAs(path);
        adapter.Insert(null, "string", "Logo", imageFolder + filename, false);
        Response.Write("<script>alert('Upload thành công');location.href='/SMO/ChucNang/Admin/Admin_QuanLyThongTinSieuThi.aspx'</script>");
        
    }
    protected void btnSaveLogo_Click(object sender, EventArgs e)
    {
        string selectLogo = Request.Form.Get("logo");
        DataDSTableAdapters.THIETLAP_WEBSITETableAdapter tlAdapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
        tlAdapter.THIETLAP_WEBSITE_setCurrentByValue("Logo", selectLogo);
        Response.Write("<script>alert('Cập nhật thành công');location.href='/SMO/ChucNang/Admin/Admin_QuanLyThongTinSieuThi.aspx'</script>");


    }
    protected void btnXoaLogo_Click(object sender, EventArgs e)
    {
        
        string selectLogo = Request.Form.Get("logo");
        DataDSTableAdapters.THIETLAP_WEBSITETableAdapter tlAdapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
        tlAdapter.THIETLAP_WEBSITE_XoaTheoGiaTriVaNhom("Logo", selectLogo);
        Response.Write("<script>alert('Xóa thành công');location.href='/SMO/ChucNang/Admin/Admin_QuanLyThongTinSieuThi.aspx'</script>");
    }
    protected void btnCapNhatThongTin_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            string tenthamso = ((Label)DataList1.Items[i].FindControl("lbTenThamSo")).Text;
            string giatri = ((TextBox)DataList1.Items[i].FindControl("txtGiaTri")).Text;
            if (giatri != "")
            {
                DataDSTableAdapters.THIETLAP_WEBSITETableAdapter tlAdapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
                tlAdapter.THIETLAP_WEBSITE_SuaTheoTenThamSo(tenthamso, giatri);
                
            }
            else
            {
                DataDSTableAdapters.THIETLAP_WEBSITETableAdapter tlAdapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
                tlAdapter.THIETLAP_WEBSITE_XoaTheoTenThamSoVaNhom(tenthamso, "ThongTinSieuThi");

            }
            Response.Write("<script>alert('Cập nhật thành công');location.href='/SMO/ChucNang/Admin/Admin_QuanLyThongTinSieuThi.aspx'</script>");
               
            

        }
            
    }
    protected void btnThemThongTin_Click(object sender, EventArgs e)
    {
        string tenthamso = txtTenThamSoThem.Text;
        string giatri = txtGiaTriThem.Text;
        DataDSTableAdapters.THIETLAP_WEBSITETableAdapter tlAdapter = new DataDSTableAdapters.THIETLAP_WEBSITETableAdapter();
        tlAdapter.Insert(tenthamso, "string", "ThongTinSieuThi", giatri, false);
        Response.Write("<script>alert('Thêm thành công');location.href='/SMO/ChucNang/Admin/Admin_QuanLyThongTinSieuThi.aspx'</script>");
    }
}
